package trunk;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Lecteur {
	
	Scanner sc;
	
	Pattern inst = Pattern.compile("(MOV|ADD|SUB|JMP|JMZ|JMN|CMP|SLT|DJN|SPL|DAT)");
	Pattern eti = Pattern.compile("(\\p{Alnum}+)");
	Pattern mode = Pattern.compile("(#|@|<)");
	Pattern valeur = Pattern.compile("(\\d|-\\d|" + eti.pattern() + ")");
	Pattern operande = Pattern.compile("(" + mode.pattern() + "?" + valeur.pattern() + ")");
	Pattern com = Pattern.compile("(;.*)");
	Pattern operation1 = Pattern.compile("((" + eti.pattern() + "\\s)?" + inst.pattern() + "\\s" + operande.pattern() + ",\\s" + operande.pattern() + "(\\s" + com.pattern() + ")?)");
	Pattern operation2 = Pattern.compile("((" + eti.pattern() + "\\s)?" + inst.pattern() + "\\s" + operande.pattern() + "(\\s" + com.pattern() + ")?)");
	Pattern operation = Pattern.compile("(" + operation1.pattern() + "|" + operation2.pattern() + ")");
	Pattern ligne = Pattern.compile("(" + com.pattern() + "|" + operation.pattern() + ")");
	
	public Lecteur(String nom) {
		try {sc = new Scanner(new File(nom));}
		catch (IOException e) {throw new IllegalArgumentException("fichier inexistant",e);}
	}
	
	public boolean hasNext() {
		return sc.hasNext();
	}
	
	public void nextLine() {
		sc.nextLine();
	}
	
	public String readLine() {
		return sc.findInLine(ligne);
	}

}